/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.web;

import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @project-name:infodb
 * @package-name:com.hyts.infodb.web
 * @author:LiBo/Alex
 * @create-date:2020-02-23 18:34
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@Controller
public class LocaleController {


    @GetMapping(value = "/locale")
    public String i18n(@RequestParam(value = "lang") String lang, HttpServletRequest request, HttpServletResponse response){
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        Locale locale = new Locale("zh","CN");
        switch (lang) {
            case "zh_CN":
                log.info("切换到中文");
                break;
            case "en_US":
                log.info("change language to English");
                locale = new Locale("en","US");
                break;
            default:
                log.info("设置默认语言为中文");
                // 默认设置中文
        }
        localeResolver.setLocale(request,response,locale);
        return "main";
    }
}
